package com.yhy.common.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;

/**
 * 默认的私信队列
 * *** 全部没有设置的都是没有持久化,并且会自动删除
 */
public class DefaultDLXConfig {

    //<editor-fold desc="默认私信队列">

    /**
     * 声明默认的死信交换机
     */
    @Bean
    public Exchange defaultDLXExchange() {
        return new DirectExchange("default.dlx.exchange", false, false);
    }

    @Bean
    public Queue defaultDLXQueue_1() {
        return QueueBuilder.nonDurable("default.dlx.queue.1").build();
    }

    @Bean
    public Queue defaultDLXQueue_2() {
        return QueueBuilder.nonDurable("default.dlx.queue.2").build();
    }

    @Bean
    public Binding defaultDLXBingDing_1(Exchange defaultDLXExchange, Queue defaultDLXQueue_1) {
        return BindingBuilder.bind(defaultDLXQueue_1).to(defaultDLXExchange)
                .with("default.dlx.routing_key.1").noargs();
    }

    @Bean
    public Binding defaultDLXBingDing_2(Exchange defaultDLXExchange, Queue defaultDLXQueue_2) {
        return BindingBuilder.bind(defaultDLXQueue_2).to(defaultDLXExchange)
                .with("default.dlx.routing_key.2").noargs();
    }
    //</editor-fold>


    // ----------------
    @Bean
    public Exchange defaultBizExchange() {
        return new DirectExchange("default.biz.exchange", false, false);
    }

    @Bean
    public Queue defaultBizQueue_1(Binding defaultDLXBingDing_1) {
        /*
         * durable: 队列名称（持久化）
         * deadLetterRoutingKey: 过期后转发到的routingKey名称
         * deadLetterExchange: 过期后转发到的exchange名称
         */
        return QueueBuilder.nonDurable("default.biz.queue.1")
                .deadLetterExchange(defaultDLXBingDing_1.getExchange())
                .deadLetterRoutingKey(defaultDLXBingDing_1.getRoutingKey())
                .build();
    }

    @Bean
    public Queue defaultBizQueue_2(Binding defaultDLXBingDing_2) {
        /*
         * durable: 队列名称（持久化）
         * deadLetterRoutingKey: 过期后转发到的routingKey名称
         * deadLetterExchange: 过期后转发到的exchange名称
         */
        return QueueBuilder.nonDurable("default.biz.queue.2")
                .deadLetterExchange(defaultDLXBingDing_2.getExchange())
                .deadLetterRoutingKey(defaultDLXBingDing_2.getRoutingKey())
                .build();
    }

    @Bean
    public Binding defaultBizBingDing_1(Exchange defaultBizExchange, Queue defaultBizQueue_1) {
        return BindingBuilder.bind(defaultBizQueue_1).to(defaultBizExchange)
                .with("default.biz.routing_key.1").noargs();
    }

    @Bean
    public Binding defaultBizBingDing_2(Exchange defaultBizExchange, Queue defaultBizQueue_2) {
        return BindingBuilder.bind(defaultBizQueue_2).to(defaultBizExchange)
                .with("default.biz.routing_key.2").noargs();
    }


}
